package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityVisitorDetailRecode;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.AES;
import com.ocom.common.utils.MD5;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.human.GuessVo;
import com.ocom.common.vo.human.UpdateVisitorRecordVo;
import com.ocom.human.dao.IDaoVisitorDetailRecode;
import com.ocom.human.service.VisitorDetailRecodeService;
//import com.ocom.human.vo.GuessVo;
//import com.ocom.human.vo.UpdateVisitorRecordVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VisitorDetailRecodeServiceImpl extends ServiceImpl<IDaoVisitorDetailRecode, EntityVisitorDetailRecode> implements VisitorDetailRecodeService {


    @Autowired
    private IDaoVisitorDetailRecode visitorDetailRecode;

    // 删除访客审批记录（DeleteVisitorDetailRecode）
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void DeleteVisitorDetailRecode(Long comId, Long id) {
        visitorDetailRecode.deleteById(id);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void AddVisitorDetailRecord(Long comId, GuessVo guessVo) {
        EntityVisitorDetailRecode entityVisitorDetailRecode = new EntityVisitorDetailRecode();
        if (StringUtils.isNotNull(comId)) {
            entityVisitorDetailRecode.setComId(comId);
        }
        if (StringUtils.isNotNull(guessVo.getRecodeId())) {
            entityVisitorDetailRecode.setRecodeId(guessVo.getRecodeId());
        }
        if (StringUtils.isNotEmpty(guessVo.getName())) {
            entityVisitorDetailRecode.setName(guessVo.getName());
        }
        if (StringUtils.isNotEmpty(guessVo.getUnit())) {
            entityVisitorDetailRecode.setUnit(guessVo.getUnit());
        }
        String key = MD5.MD5Encode(comId.toString(), "utf8");
        if (StringUtils.isNotEmpty(guessVo.getIdentity())) {
            entityVisitorDetailRecode.setEncryIdentity(AES.encrypt(guessVo.getIdentity(), key));
            entityVisitorDetailRecode.setIdentity(StringUtils.desValue(guessVo.getIdentity(), 4, 4, "*"));
        }
        if (StringUtils.isNotEmpty(guessVo.getPhone())) {
            entityVisitorDetailRecode.setPhone(AES.encrypt(guessVo.getPhone(), key));
            entityVisitorDetailRecode.setPhone(StringUtils.desValue(guessVo.getPhone(), 4, 4, "*"));
        }

        entityVisitorDetailRecode.setApproverStatus(0L);
        visitorDetailRecode.insert(entityVisitorDetailRecode);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void UpdateVisitorRecord(Long comId, UpdateVisitorRecordVo updateVisitorRecordVo) {
        UpdateWrapper<EntityVisitorDetailRecode> update = Wrappers.update();
        List<String> ids = Arrays.stream(updateVisitorRecordVo.getDetailIds().split(",")).collect(Collectors.toList());
        update.in("id",ids);
        if (StringUtils.isNotNull(updateVisitorRecordVo.getApproverStatus()) && updateVisitorRecordVo.getApproverStatus()>-1){
            update.set("approver_status",updateVisitorRecordVo.getApproverStatus());
        }
        update.set("update_time",new Date());
        this.update(update);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Integer selectCount(Long comId, Long recodeId) {
        QueryWrapper<EntityVisitorDetailRecode> query = Wrappers.query();
        query.eq("id",recodeId);
        query.eq("approver_status",0);
        return visitorDetailRecode.selectCount(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityVisitorDetailRecode> DetailVisitorInfo(Long comId, Long recodeId) {
        QueryWrapper<EntityVisitorDetailRecode> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("recode_id",recodeId);
        return visitorDetailRecode.selectList(query);
    }
}
